using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Globalization;

using com.Yous.SPMS.Common;


namespace SpmsConfig
{
    public partial class SpmsSetter : Form
    {
        SqlConnectionStringBuilder builder;

        public SpmsSetter()
        {
            InitializeComponent();
        }

        private void SpmsSetter_Load(object sender, EventArgs e)
        {
            label15.Text = string.Format(CultureInfo.CurrentCulture,"Now is {0}", DateTime.Now);

            builder = new SqlConnectionStringBuilder(SPMSConfiguration.DbConnectionString);
            this.txtDbServer.Text = builder.DataSource;
            this.txtDbName.Text = builder.InitialCatalog;
            this.txtDbUser.Text = builder.UserID;
            this.txtDbPassword.Text = builder.Password;

            //---------------
            this.txtSmtpServer.Text = SPMSConfiguration.SmtpServer;
            this.txtPort.Text = SPMSConfiguration.SmtpPort;
            this.txtSender.Text = SPMSConfiguration.MailSender;
            this.txtAccountName.Text = SPMSConfiguration.SmtpCredential;
            this.txtSmtpPassword.Text = SPMSConfiguration.CredentialPassword;
            if (string.IsNullOrEmpty(this.txtAccountName.Text))
            {
                this.checkAuthentication.Checked = false;
            }
            else
            {
                this.checkAuthentication.Checked = true;
            }
        }

        private void btSaveDb_Click(object sender, EventArgs e)
        {
            ApplyToConnBuilder();

            if (SPMSConfiguration.SaveDbConnectionString(builder))
            {
                MessageBox.Show("Database connection saved!");
            }
            else
            {
                MessageBox.Show("Maybe cann't access xml file,or format error!", "Save Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void ApplyToConnBuilder()
        {
            this.builder.DataSource = this.txtDbServer.Text.Trim();
            this.builder.InitialCatalog = this.txtDbName.Text.Trim();
            this.builder.UserID = this.txtDbUser.Text.Trim();
            this.builder.Password = this.txtDbPassword.Text.Trim();
        }

        private void btSaveMail_Click(object sender, EventArgs e)
        {

            SPMSConfiguration.SmtpServer = txtSmtpServer.Text.Trim();
            SPMSConfiguration.SmtpPort = txtPort.Text.Trim();

            if (string.IsNullOrEmpty(SPMSConfiguration.SmtpPort))
            {
                SPMSConfiguration.SmtpPort = "25";
            }

            SPMSConfiguration.MailSender = txtSender.Text.Trim();
            if (this.checkAuthentication.Checked)
            {
                SPMSConfiguration.CredentialPassword = txtSmtpPassword.Text.Trim();
                SPMSConfiguration.SmtpCredential = txtAccountName.Text.Trim();
            }
            else
            {
                SPMSConfiguration.CredentialPassword = string.Empty;
                SPMSConfiguration.SmtpCredential = string.Empty;
            }

            SPMSConfiguration.SaveMailConfig();
            MessageBox.Show("Mail & SMTP saved!");
        }

        private void checkAuthentication_CheckedChanged(object sender, EventArgs e)
        {
            this.txtAccountName.Enabled = this.checkAuthentication.Checked;
            this.txtSmtpPassword.Enabled = this.checkAuthentication.Checked;
        }

        private void txtPort_TextChanged(object sender, EventArgs e)
        {
            int p;
            if (int.TryParse(txtPort.Text, out p) == false)
            {
                MessageBox.Show("Please enter a number");
            }
        }

        private void btTestConnection_Click(object sender, EventArgs e)
        {
            ApplyToConnBuilder();
            SqlConnection con = new SqlConnection(builder.ConnectionString);
            btTestConnection.Enabled = false;
            labWait.Visible = true;
            labWait.Refresh();
            Cursor cur = this.Cursor;
            this.Cursor = Cursors.WaitCursor;

            try
            {
                con.Open();
                MessageBox.Show("Connection Passed");
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message, "Connection Fail", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            this.Cursor = cur;
            btTestConnection.Enabled = true;
            labWait.Visible = false;
        }

        private void tabDatabase_Click(object sender, EventArgs e)
        {

        }
    }
}